import express from "express";
import { createServer } from 'http';

import cors from "cors";

import { Server } from 'socket.io';
const app = express();
app.use(cors());
const httpServer = createServer(app);
const io = new Server(httpServer, {
    cors: {
        origin: "http://localhost:5173", // 替换为你的前端地址
        methods: ["GET", "POST"]
    }
});

io.on('connection', (socket) => {
    console.log('新用户连接:', socket.id);

    // 接收音频消息并广播给其他用户
    socket.on('audio-message', (audioData) => {
        socket.broadcast.emit('audio-message', audioData);
    });

    socket.on('disconnect', () => {
        console.log('用户断开连接:', socket.id);
    });
});

const PORT = process.env.PORT || 3001;
httpServer.listen(PORT, () => {
    console.log(`服务器运行在 http://localhost:${PORT}`);
});